/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.collector.file;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-19 上午9:45:58          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class DirFilesNumber extends Collector {

    private static Logger logger = Logger.getLogger(DirFilesNumber.class);

    @Override
    public String[] getPropMeta() {
        return new String[] { "NUMBER" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DIR", "EXPR" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        String[] propMetas = getPropMeta();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String dir = this.getParamMap().get("DIR").trim();
        String expr = this.getParamMap().get("EXPR").trim();
        String cmd = getCmd(dir, expr);
        BufferedReader buffReader = null;
        try {
            logger.error("begin fileNumber>>>ip[" + ip + "] cmd[" + cmd + "] ");
            buffReader = CommandReader.getReader(ip, cmd, 0, 0);
            String strLine = "";
            while ((strLine = buffReader.readLine()) != null) {
                propValues[0] = ResPropertyValue.createPropValue(String.valueOf(strLine.trim()), propMetas[0]);
                propertyValueX.setValues(propValues);
                logger.error("end fileNumber>>>ip[" + ip + "] cmd[" + cmd + "] strLine[" + strLine + "]");
            }
            values.add(propertyValueX);
        } catch (IOException e) {
            propValues[0] = ResPropertyValue.createPropValue("0", propMetas[0]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } finally {
            if (buffReader != null) {
                try {
                    buffReader.close();
                } catch (IOException e) {
                    logger.error("failed to close buffReader", e);
                }
            }
        }

        return values;
    }

    private String getCmd(String dir, String expr) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("ls -l ");
        buffer.append(dir);
        buffer.append("|");
        buffer.append(expr);
        buffer.append(" ");
        buffer.append("|wc -l");
        return buffer.toString();
    }
}
